package com.example.backend.controller;


import com.example.backend.model.BaseModel;
import com.example.backend.model.SignModel;
import com.example.backend.service.SignService;
import com.fasterxml.jackson.databind.ser.Serializers;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;

@RestController
@CrossOrigin
@RequestMapping("sign")
public class SignController {
    @Resource
    private SignService signService;

    @Resource
    HttpServletRequest request;

    @RequestMapping(value = "insertSign", method = RequestMethod.POST)
    public BaseModel insertSign() {
        int userId = Integer.parseInt(request.getParameter("userId"));
        String lastDate = request.getParameter("lastDate");
        if (signService.insertSign(userId, lastDate))
            return new BaseModel(0, "success");
        else
            return new BaseModel(1, "error");
    }

    @RequestMapping(value = "updateSign", method = RequestMethod.POST)
    public BaseModel updateSign() {
        int userid = Integer.parseInt(request.getParameter("userid"));
        String lastDate = request.getParameter("lastDate");
        if (signService.updateSign(userid, lastDate))
            return new BaseModel(0, "success");
        else
            return new BaseModel(1, "error");
    }

    @RequestMapping(value = "Sign", method = RequestMethod.POST)
    public SignModel Sign() {
        int userId = Integer.parseInt(request.getParameter("userId"));
        String lastDate = request.getParameter("lastDate");
        if (signService.ifSign(userId)) {
            if (signService.updateSign(userId, lastDate))
                return new SignModel(0, "update success");
            else
                return new SignModel(1, "update error");
        }
        else {
            if (signService.insertSign(userId, lastDate))
                return new SignModel(0, "insert success");
            else
                return new SignModel(1,"insert error");
        }
    }
}